﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;



namespace GestorDeFlotasDesktop.AbmAuto
{
    public partial class AbmAuto : Form
    {
        String conexion;
        public AbmAuto()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            this.rellenarComboBoxMarcas();
            this.rellenarComboBoxModelosReloj();
        }

        public void rellenarComboBoxModelosReloj()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_seleccionar_reloj_habilitado";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            modeloRelojComboBox.DataSource = dt;
                            modeloRelojComboBox.DisplayMember = "nro_serie";

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        public void rellenarComboBoxMarcas(){
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_marca_automovil";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            marcaComboBox.DataSource = dt;
                            marcaComboBox.DisplayMember = "descripcion";

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al cargar marcas de automovil");
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (marcaComboBox.Text != string.Empty && licenciaTextBox.Text != string.Empty && modeloRelojComboBox.Text != string.Empty && modeloRelojComboBox.Text != string.Empty && patenteTextBox.Text != string.Empty && rodadoTextBox.Text != string.Empty && modeloRelojComboBox.Text != string.Empty)
            {
                    using (SqlConnection connection = new SqlConnection(conexion))
                    {
                        try
                        {
                            using (SqlCommand command = new SqlCommand())
                            {
                                connection.Open();
                                command.Connection = connection;
                                command.CommandType = System.Data.CommandType.StoredProcedure;
                                command.CommandText = "TEQUILASO.sp_altaAutomovil";
                                command.Parameters.AddWithValue("@Descripcion", marcaComboBox.Text);
                                command.Parameters.AddWithValue("@Licencia", licenciaTextBox.Text);
                                command.Parameters.AddWithValue("@Rodado", rodadoTextBox.Text);
                                command.Parameters.AddWithValue("@Modelo", modeloTextBox.Text);
                                command.Parameters.AddWithValue("@Patente", patenteTextBox.Text);
                                command.Parameters.AddWithValue("@Reloj_nro_serie", modeloRelojComboBox.Text);
                                command.ExecuteReader();
                                connection.Close();

                                MessageBox.Show("Alta realizada con exito");
                                

                            }
                        }
                        catch (SqlException  ex)
                        {
                            MessageBox.Show(ex.Message,"Alta auto - error");
                              
                        }
                     }
            
                }
                else{
                    MessageBox.Show("Todos los campos deben estar completos","Error"); 
                }


        }
    }
 }
